package com.dongnam.quanly.common.controller;

import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.dao.HoaDonDAO;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.ChiTietDonHang;
import com.dongnam.quanly.common.model.DoiTac;
import com.dongnam.quanly.common.model.HoaDon;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class HoaDonController extends GenericController<HoaDon, String> {

	private static final Logger logger = LoggerFactory.getLogger(HoaDonController.class);

	public HoaDonController() {
		super(new HoaDonDAO());
		// TODO Auto-generated constructor stub
	}

	@Override
	public HoaDonDAO getDao() {
		return (HoaDonDAO) dao;
	}

	public List<String> getListHoaDon(String sodonhang) throws DatabaseException {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<String> list = getDao().getListHoaDon(sodonhang);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

	public boolean checkHd(String sohoadon) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			boolean check = getDao().checkHoaDon(sohoadon);
			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return check;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}
}
